<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="账号">
        <el-input
          v-model="searchObj.userName"
          placeholder="请输入账号"
          clearable
          @change="fetchData()"
          @keyup.enter="fetchData()"
        />
      </el-form-item>

      <el-form-item label="角色">
        <el-select v-model="searchObj.role" placeholder="请选择" clearable @change="fetchData">
          <el-option label="管理员" value="0" />
          <el-option label="普通用户" value="1" />
        </el-select>
      </el-form-item>

      <el-form-item label="负责人">
        <el-select v-model="searchObj.person" placeholder="请选择" clearable @change="fetchData">
          <el-option
            v-for="item in personList"
            :key="item.person"
            :label="item.person"
            :value="item.person"
          />
        </el-select>
      </el-form-item>

      <br>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="fetchData">
          查询
        </el-button>
        <el-button type="primary" icon="el-icon-delete" size="small" @click="resetData">
          重置
        </el-button>
        <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="dialogInsertVisable = true">
          新增
        </el-button>
      </el-form-item>

      <!--      <span style="line-height: 0px; margin-left: 1385px">-->
      <!--        <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="dialogInsertVisable = true">-->
      <!--          新增-->
      <!--        </el-button>-->
      <!--      </span>-->

    </el-form>

    <!-- 新增信息弹窗 -->
    <el-dialog title="新增用户信息" :visible.sync="dialogInsertVisable" @close="closeInsertVisable('insertObj')">
      <el-form ref="insertObj" :model="insertObj" label-width="100px" class="demo-form-inline" :rules="rules">
        <el-row :gutter="20">
          <el-col :span="11">
            <el-form-item label="账号" prop="userName">
              <el-input v-model="insertObj.userName" placeholder="请输入账号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="密码" prop="password">
              <el-input v-model="insertObj.password" show-password placeholder="请输入密码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="角色" prop="role">
              <el-select v-model="insertObj.role" placeholder="请选择" clearable>
                <el-option label="管理员" value="0" />
                <el-option label="普通用户" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="负责人" prop="person">
              <el-input v-model="insertObj.person" placeholder="请输入负责人" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogInsertVisable = false">
          取消
        </el-button>
        <el-button type="primary" @click="insertSubmit">
          确定
        </el-button>
      </div>
    </el-dialog>

    <!--列表 height="600"-->
    <el-table :data="list" border stripe width="50%">
      <el-table-column label="序号" width="50">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="userName" label="账号" />
      <el-table-column prop="role" label="角色">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.role === 0" type="success" size="mini">
            管理员
          </el-tag>
          <el-tag v-else type="info" size="mini">
            普通用户
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="person" label="负责人" />

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="editById(scope.row.id)"
          >
            修改
          </el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="foreverRemove(scope.row.id)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 修改信息弹窗 -->
    <el-dialog title="修改用户信息" :visible.sync="dialogTableVisible" width="1000px" @close="closeDetailVisable('detailObj')">
      <el-form ref="detailObj" :model="detailObj" label-width="100px" class="demo-form-inline" :rules="rules">
        <el-row :gutter="20">
          <el-col :span="11">
            <el-form-item label="账号" prop="userName">
              <el-input v-model="detailObj.userName" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="密码" prop="password">
              <el-input v-model="detailObj.password" show-password />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="角色" prop="role">
              <el-select v-model="detailObj.role" placeholder="请选择" clearable>
                <el-option label="管理员" value="0" />
                <el-option label="普通用户" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="负责人" prop="person">
              <el-input v-model="detailObj.person" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible = false; detailObj = {}">
          取消
        </el-button>
        <el-button type="primary" @click="detailSubmit">
          确定
        </el-button>
      </div>
    </el-dialog>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[10, 20, 50, 100]"
      style="padding: 30px 0; "
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changePageSize"
      @current-change="changeCurrentPage"
    />

  </div>
</template>

<script>
/* eslint-disable */
import userAdminApi from '@/api/userAdmin/userAdmin'
import crawlerApi from "@/api/crawler/crawler";

export default {
  name: 'UserInfoList',
  data() {
    return {
      list: null, // 数据列表
      total: 0, // 数据库总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      // 负责人列表
      personList: [],
      // 查询条件
      searchObj: {},
      // 修改条件
      detailObj: {
        userName: '',
        password: ''
      },
      // 新增条件
      insertObj: {
        userName: '',
        password: ''
      },
      dialogTableVisible: false, // 修改对话框是否显示
      dialogInsertVisable: false, // 新增对话框是否显示
      rules: {
        userName: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        role: [{ required: true, message: '请选择角色', trigger: 'change' }],
        person: [{ required: true, message: '请输入负责人', trigger: 'blur' }]
      }

    }
  },

  created() {
    this.fetchData()
    this.fetchPersonList()
  },

  methods: {
    fetchData() {
      userAdminApi.listPage(this.page, this.limit, this.searchObj).then((response) => {
        this.list = response.data.result.list
        this.total = response.data.result.total
      })
    },

    fetchPersonList() {
      crawlerApi.getPersonList().then((response) => {
        this.personList = response.data.result
      })
    },

    fetchDataById(id) {
      userAdminApi.getInfoById(id).then((response) => {
        this.detailObj = response.data.result
      })
    },

    // 每页展示条数的改变，size：回调函数, 表示当前选中的展示条数
    changePageSize(size) {
      this.limit = size
      this.fetchData()
    },

    // 翻页，page：回调函数, 表示当前选中的页码
    changeCurrentPage(page) {
      this.page = page
      this.fetchData()
    },

    // 重置搜索表单
    resetData() {
      this.searchObj = {}
      this.page = 1
      this.limit = 10
      this.fetchData()
    },

    // 关闭新增弹窗
    closeInsertVisable(insertObj) {
      this[insertObj] = {
        userName: '',
        password: ''

      }
      this.$nextTick(() => {
        this.$refs[insertObj].clearValidate()
      })
    },

    // 新增
    insertSubmit() {
      this.$refs.insertObj.validate(valid => {
        if (valid) {
          userAdminApi.insertUserInfo(this.insertObj).then((response) => {
            this.$message.success(response.message)
            this.fetchData()
          }).catch(() => {
            this.$message.error('新增失败')
          })
          this.dialogInsertVisable = false
        } else {
          return false
        }
      })
    },

    // 修改用户信息对话框
    editById(id) {
      // 根据id查询数据
      this.fetchDataById(id)

      // 打开对话框
      this.dialogTableVisible = true
    },

    // 关闭修改弹窗
    closeDetailVisable(detailObj) {
      this[detailObj] = {
        userName: '',
        password: ''

      }
      this.$nextTick(() => {
        this.$refs[detailObj].clearValidate()
      })
    },

    // 修改用户信息
    detailSubmit(id) {
      this.$refs.detailObj.validate(valid => {
        if (valid) {
          userAdminApi.updateById(this.detailObj).then((response) => {
            this.$message.success(response.message)
            this.fetchData()
          }).catch(() => {
            this.$message.error('修改失败')
          })
          this.dialogTableVisible = false
        } else {
          return false
        }
      })
    },

    // 永久删除用户信息
    foreverRemove(id) {
      this.$confirm('此操作将永久删除该记录且无法恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'waring'
      })
        .then(() => {
          userAdminApi.foreverRemove(id).then(response => {
            this.$message.success(response.message)
            this.fetchData()
          })
        })
        .catch(() => {
          this.$message.info('取消删除')
        })
    }

  }
}
</script>

<style scoped>

</style>
